package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.entity.Dish;
import cn.usian.entity.DishDto;
import cn.usian.service.CategoryService;
import cn.usian.service.DishFlavorService;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @RequestMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        return dishService.page(page,pageSize,name);
    }
    @DeleteMapping
    public R delete(Long[] ids){
        dishService.delete(ids);
        return R.success("删除成功");
    }
    @PostMapping("status/{status}")
    public R status(@PathVariable("status") Integer status,Long[] ids){
        dishService.status(status,ids);
        return R.success("修改状态成功");
    }


    @PostMapping
    public R add(@RequestBody DishDto dishDto, HttpSession session){
        Long id = (Long)session.getAttribute("employee");
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);
        dishService.add(dishDto);
//        Long id=dishService.selectByName(dishDto.getName());
//        List<DishFlavor> flavor = dishDto.getFlavors();
//        for (int i = 0; i < flavor.size(); i++) {
//            DishFlavor dishFlavor = new DishFlavor();
//            dishFlavor.setDishId(id);
//            dishFlavor.setName(flavor.get(i).getName());
//            dishFlavor.setValue(flavor.get(i).getValue());
//            dishFlavorService.add(dishFlavor,session);
//        }
        return R.success("成功");
    }
    @GetMapping("{id}")
    public R findByid(@PathVariable Long id){
        return dishService.findByid(id);
    }
    @GetMapping("/list")
    public R<List<Dish>> list(Long categoryId,String name){
        return R.success(dishService.list(categoryId,name));
    }
}
